package com.project.frba.bifront.client.view.properties;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;


public class PropertyPanel extends Composite{
	
	private Panel mainPanel = new Panel();
	ListBox lbProperties = new ListBox();
	Button up = new Button();
	Button down = new Button();
	Button del = new Button();
	PropertySelector parent;
	
	public PropertyPanel() {
		
	}
	
	public PropertyPanel(String name, List<String> properties, PropertySelector parent){
		//mainPanel.setTitle(name);
		mainPanel.setLayout(new HorizontalLayout(4));
		
		FlexTable tableLayout = new FlexTable();
		tableLayout.setHTML(0, 0, "<b style=\"font-size: 11px\">" + name + "</b>");
		tableLayout.setWidget(1, 0, lbProperties);
		mainPanel.add(tableLayout);
				
		lbProperties.setVisibleItemCount(5);
		lbProperties.setWidth("250");
		Panel buttonsPanel = new Panel();
		buttonsPanel.setLayout(new VerticalLayout(3));
		up.setIcon("./icons/up.png");
		buttonsPanel.add(up);
		down.setIcon("./icons/down.png");
		buttonsPanel.add(down);
		del.setIcon("./icons/remove.png");
		buttonsPanel.add(del);
		
		
		mainPanel.add(buttonsPanel);
		
		up.addListener(new ButtonListenerAdapter(){
			@Override
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				moveUp();
			}
		});
		
		down.addListener(new ButtonListenerAdapter(){
			@Override
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				moveDown();
			}
		});
		
		del.addListener(new ButtonListenerAdapter(){
			@Override
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				deleteProperty();
			}
		});
		
		
		this.parent = parent;
		for (final String propertyName : properties) {
			this.addProperty(propertyName);
		}
		initWidget(mainPanel);
	}
	
	public void addProperty(String propertyName){
		lbProperties.addItem(parent.getLabelForProperty(propertyName), propertyName);
	}
	
	public void deleteProperty(){
		String propertyName = lbProperties.getValue(lbProperties.getSelectedIndex());
		lbProperties.removeItem(this.getIndexOf(propertyName));
		parent.freeProperty(propertyName);
	}
	
	private Integer getIndexOf(String propertyName){
		for (int i = 0; i < lbProperties.getItemCount(); i++) {
			if (lbProperties.getValue(i).equals(propertyName)) {
				return i;
			}
		}
		return null;
	}
	
	public void moveUp(){
		Integer actualPosition = lbProperties.getSelectedIndex();
		Integer previousPosition = actualPosition-1;
		if (actualPosition > 0) {
			String previousPropertyName = this.lbProperties.getValue(previousPosition);
			this.lbProperties.removeItem(previousPosition);
			this.lbProperties.insertItem(this.parent.getLabelForProperty(previousPropertyName), previousPropertyName, actualPosition);
			this.lbProperties.setSelectedIndex(previousPosition);
		}
	}
	
	public void moveDown(){
		Integer actualPosition = lbProperties.getSelectedIndex();
		Integer nextPosition = actualPosition + 1;
		if (actualPosition < lbProperties.getItemCount()) {
			String propertyName = this.lbProperties.getValue(actualPosition);
			this.lbProperties.removeItem(actualPosition);
			this.lbProperties.insertItem(this.parent.getLabelForProperty(propertyName), propertyName, nextPosition);
			this.lbProperties.setSelectedIndex(nextPosition);
		}
		
	}

	public List<String> getProperties() {
		List<String> properties = new ArrayList<String>();
		for (int i = 0; i < lbProperties.getItemCount(); i++) {
			properties.add(lbProperties.getValue(i));
		}
		return properties;
	}
	
	public void doFilter(){
		
	}
	
}
